﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using umbraco.cms.businesslogic.media;
using Umbraco.Forms.Data;

namespace ContourStrikesAgain.FieldTypes
{
    public class MediaRadioButtonList : FieldType
    {
        private System.Web.UI.WebControls.RadioButtonList rbl;

        [Umbraco.Forms.Core.Attributes.Setting("MediaFolder", 
            description = "Select the media folder",
            control = "ContourStrikesAgain.FieldSettingTypes.MediaPicker",
            assembly = "ContourStrikesAgain")]
        public string MediaFolder { get; set; }

      

        public MediaRadioButtonList()
        {
            this.Id = new Guid("F459EF9D-7DFD-4705-BB04-61DD1DA0F82A");
            this.Name = "Media RadioButtonList";
            this.Description = "Renders a radiobutton list with image";

            this.Icon = "radiobuttonlist.png";
            this.DataType = FieldDataType.String;
            rbl = new System.Web.UI.WebControls.RadioButtonList();
            _value = new List<object>();
        }


        private List<Object> _value = new List<object>();
        public override List<object> Values
        {
            get
            {
                if (rbl.SelectedValue != "")
                {
                    _value.Clear();
                    _value.Add(rbl.SelectedItem.Value);
                }

                return _value;
            }
            set
            {
                _value = value;
            }
        }

        public override System.Web.UI.WebControls.WebControl Editor
        {
            get
            {
                rbl.RepeatLayout = System.Web.UI.WebControls.RepeatLayout.Flow;
                rbl.CssClass = "radiobuttonlist";


                if (!string.IsNullOrEmpty(MediaFolder))
                {
                    Media m = new Media(Convert.ToInt32(MediaFolder));

                    foreach (Media image in m.Children)
                    {
                        string caption = string.Format(
                           "<img src=\"{0}\" class=\"radiobuttonimage\" /><span class=\"radiobuttonimagetext\">{1}</span>",
                            image.getProperty("umbracoFile").Value.ToString(),
                            DictionaryHelper.GetText(image.Text));

                        System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(caption, image.Text);

                        if (_value.Contains(image.Text))
                            item.Selected = true;

                        rbl.Items.Add(item);
                    }
                }

                return rbl;
            }
            set
            {
                base.Editor = value;
            }
        }

        public override string RenderPreview()
        {
            return "Preview not available";
        }

        public override string RenderPreviewWithPrevalues(List<object> prevalues)
        {
            return RenderPreview();
        }

    }
}